package Offer;

public class Test27 
{

	public static void main(String[] args) 
	{

	}
     public BinaryTreeNode ConvertBSTToBiList(BinaryTreeNode root)
     {
    	 if(root==null)
    	   return null;
    	 root.left = max(root.left);
    	 if(root.left!=null)
    	 {
    		 root.left.right=root;
    	 }
    	 root.right=min(root.right);
    	 if(root.right!=null)
    	 {
    		 root.right.left=root;
    	 }  	 
    	 while(root.left!=null)
    	 {
    		 root=root.left;
    	 }
    	 return root;
     }
     public static BinaryTreeNode max(BinaryTreeNode root)
     {
    	 if(root==null)
    		 return null;
    	 root.left=max(root.left);
    	 if(root.left!=null)
    	 {
    		 root.left.right = root;
    	 }
    	 root.right=min(root.right);
    	 if(root.right!=null)
    	 {
    		 root.right.left=root;
    	 }
    	 while(root.right!=null)
    	 {
    		 root=root.right;
    	 }
    	 return root;
     }
     public static BinaryTreeNode min(BinaryTreeNode root)
     {
    	 if(root==null)
    		 return null;
    	 root.left=max(root.left);
    	 if(root.left!=null)
    	 {
    		 root.left.right = root;
    	 }
    	 root.right=min(root.right);
    	 if(root.right!=null)
    	 {
    		 root.right.left=root;
    	 }
    	 while(root.left!=null)
    	 {
    		 root=root.left;
    	 }
    	 return root;
     }
}	
